package com.san.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzk.vo.CaseUser;
import com.hzk.vo.RespBean;
import com.hzk.vo.RespBeanEnum;
import com.san.mapper.ExchangMapper;
import com.san.pojo.Exchang;
import com.san.pojo.TaomiGoods;
import com.san.pojo.UserBasic;
import com.san.service.ITaomiGoodsService;
import com.san.service.IUserBasicService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jack
 * @since 2022-04-01
 */
@Controller
@RequestMapping("/taomi-goods")
public class TaomiGoodsController {
    @Autowired
    ITaomiGoodsService itaomiGoodsService;
    @Autowired
    IUserBasicService iUserBasicService;
    @Autowired
    ExchangMapper exchangMapper;
    @Autowired
    RedisTemplate redisTemplate;

    /*  @RequestMapping("/toList")
      @ResponseBody
      public RespBean toList(HttpServletRequest request,){
          String token = request.getHeader("token");
          UserBasic user = (UserBasic) redisTemplate.opsForValue().get("UserBasic:" + token);
          if (user==null){
              return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
          }
          List<TaomiGoods> taomiGoods = itaomiGoodsService.toList();
          if (taomiGoods==null){
              return RespBean.error(RespBeanEnum.NULL_TAOMI_GOODS);
          }
          return RespBean.success(taomiGoods);
      }*/
    @RequestMapping("/toList")
    @ResponseBody
    public RespBean toList(@CaseUser UserBasic userBasic ) {
        if (userBasic==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        List<TaomiGoods> taomiGoods = itaomiGoodsService.toList();
        if (taomiGoods == null) {
            return RespBean.error(RespBeanEnum.NULL_TAOMI_GOODS);
        }
        return RespBean.success(taomiGoods);
    }

    @RequestMapping("/select")
    @ResponseBody
    public RespBean toList(@CaseUser com.hzk.pojo.UserBasic userBasic, String name) {

        if (userBasic==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        List<TaomiGoods> taomiGoods = itaomiGoodsService.select(name);
        if (taomiGoods==null) {
            return RespBean.error(RespBeanEnum.NULL_TAOMI_GOODS);
        }
        return RespBean.success(taomiGoods);
    }

    @RequestMapping("/exchange")
    @ResponseBody
    public RespBean exchange( Long id, String username, Long phone) {
       /* if (userBasic==null){Integer taomi1 = userBasic.getTaomi();
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }*/
        TaomiGoods byId = itaomiGoodsService.getById(id);//需要的淘米
        UserBasic byId1 = iUserBasicService.getById(phone);//用户的淘米
        Integer taomi1 = byId1.getTaomi();//用户的淘米
        Long taomi = byId.getTaomi();//需要的淘米
        if (taomi > taomi1) {
            return RespBean.error(RespBeanEnum.NULL_TAOMI_COUNT_ERROR);
        }
        boolean id1 = itaomiGoodsService.update(new UpdateWrapper<TaomiGoods>().eq("id", id)
                .setSql("total=" + "total-1"));

        byId1.setTaomi((int) (byId1.getTaomi()-taomi));
        boolean b = iUserBasicService.updateById(byId1);
        Exchang exchang = new Exchang();
        exchang.setPhone(phone);
        exchang.setTaominame(byId.getGoodsname());
        exchang.setUsername(username);
        exchangMapper.insert(exchang);
        return RespBean.success(RespBeanEnum.TAOMI_);
    }
}
